﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DoeLibsMVC.Models;
using DoeObjects;

namespace DoeLibsMVC.Controllers
{
    public class TitleController : BaseController
    {
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Get(int id)
        {
            ViewBag.titleId = id;

            return View();
        }

        [Authorize(Roles = Security.UserCategoryHelper.STAFF_CATEGORY)]
        public ActionResult AddLoanable(int id)
        {
            ViewBag.titleId = id;
            EditLoanableBasicModel model = new EditLoanableBasicModel();
            model.Location = User.Room;
            
            return View(model);
        }

		
		[HttpGet]
        [Authorize(Roles=Security.UserCategoryHelper.STAFF_CATEGORY)]
		public ActionResult Edit(int id)
		{
            Title title = Title.getById(id);
			EditTitleModel model = new EditTitleModel();

			if (title != null)
			{
				model.ISBN10 = title.ISBN10;
                model.ISBN13 = title.ISBN13;
				model.Title = title.BookTitle;
				model.Edition = title.EditionNumber;
				model.PublicationYear = title.EditionYear;
				model.FirstEditionYear = title.FirstEditionYear;
				model.PublisherObject = title.Publisher;
                model.AuthorList = Author.getAuthorsFromBook(id);
                model.EditorList = Author.getEditorsFromBook(id);
				model.TopicList = Topic.getTopicsFromBook(id);

                ViewBag.titleId = title.TitleId;
			}
            else
            {
                return new HttpStatusCodeResult(System.Net.HttpStatusCode.NotFound);
            }

			return View(model);
		}

        //[HttpPost]
        //[Authorize(Roles = Security.UserCategoryHelper.STAFF_CATEGORY)]
        //public ActionResult Edit(EditTitleModel model, int id)
        //{
        //    Title title = Title.getById(id);

        //    if (title == null)
        //    {
        //        ViewBag.success = false;
        //        ViewBag.message = "No title with the given ID found.";
        //        return View(model);
        //    }

        //    if (string.IsNullOrEmpty(model.ISBN10) && string.IsNullOrEmpty(model.ISBN13))
        //    {
        //        ModelState.AddModelError("ISBN10", "At least one ISBN number must be specified.");
        //        ModelState.AddModelError("ISBN13", "At least one ISBN number must be specified.");
        //    }

        //    //check that the ISBNs not in use or only in use for the title which is edited...
        //    Title titleWithIsbn10 = Title.getByIsbn10(model.ISBN10);
        //    Title titleWithIsbn13 = Title.getByIsbn13(model.ISBN13);

        //    if (!string.IsNullOrEmpty(model.ISBN10) && titleWithIsbn10 != null && titleWithIsbn10.TitleId != id)
        //    {
        //        ModelState.AddModelError("ISBN10", "This ISBN is already in use by another title.");
        //    }
        //    if (!string.IsNullOrEmpty(model.ISBN13) && titleWithIsbn13 != null && titleWithIsbn13.TitleId != id)
        //    {
        //        ModelState.AddModelError("ISBN13", "This ISBN is already in use by another title.");
        //    }



        //    if (ModelState.IsValid)
        //    {
        //        title.ISBN10 = model.ISBN10;
        //        title.ISBN13 = model.ISBN13;
        //        title.BookTitle = model.Title;
        //        title.EditionNumber = model.Edition;
        //        title.EditionYear = model.PublicationYear;
        //        title.FirstEditionYear = model.FirstEditionYear;
        //        title.Publisher = model.Publisher;

        //        //set the authors:
        //        //remove all first
        //        Title.deleteAllAuthorTitleReferences(id);
        //        //add all
        //        foreach (Author a in model.Authors)
        //            Title.addAuthor(a.Aid, id);

        //        Title.deleteAllTopicTitleReferences(id);
        //        foreach (Topic t in model.Topics)
        //            Title.addTopic(t.TopicId, id);

        //        string message;
        //        if (title.Publisher.PublisherId > 0)
        //        {
        //            if (title.save())
        //            {
        //                base.Message = new HttpMessage("Title has been edited.", true);
        //                return RedirectToAction("Get", "Title", new { id = id });
        //            }
        //            else 
        //            {
        //                base.Message = new HttpMessage("Technical problems during save. Please try again.", false);
        //            } 
        //        }
        //        else
        //        {
        //            base.Message = new HttpMessage("Publisher could not be added.", false);
        //        }
        //    }
        //    return View(model);
        //}


        //[HttpGet]
        //[Authorize(Roles=Security.UserCategoryHelper.STAFF_CATEGORY)]
        //public ActionResult Add()
        //{
        //    return View(new EditTitleModel());
        //}

        //[HttpPost]
        //[Authorize(Roles = Security.UserCategoryHelper.STAFF_CATEGORY)]
        //public ActionResult Add(EditTitleModel model)
        //{
        //    if (string.IsNullOrEmpty(model.ISBN10) && string.IsNullOrEmpty(model.ISBN13))
        //    {
        //        ModelState.AddModelError("ISBN10", "At least one ISBN number must be specified.");
        //        ModelState.AddModelError("ISBN13", "At least one ISBN number must be specified.");
        //    }

        //    //check that the ISBNs not in use or only in use for the title which is edited...
        //    Title titleWithIsbn10 = Title.getByIsbn10(model.ISBN10);
        //    Title titleWithIsbn13 = Title.getByIsbn13(model.ISBN13);

        //    if (!string.IsNullOrEmpty(model.ISBN10) && titleWithIsbn10 != null)
        //    {
        //        ModelState.AddModelError("ISBN10", "This ISBN is already in use by another title.");
        //    }
        //    if (!string.IsNullOrEmpty(model.ISBN13) && titleWithIsbn13 != null)
        //    {
        //        ModelState.AddModelError("ISBN13", "This ISBN is already in use by another title.");
        //    }



        //    if (ModelState.IsValid)
        //    {
        //        Title title = new Title();
        //        title.ISBN10 = model.ISBN10;
        //        title.ISBN13 = model.ISBN13;
        //        title.BookTitle = model.Title;
        //        title.EditionNumber = model.Edition;
        //        title.EditionYear = model.PublicationYear;
        //        title.FirstEditionYear = model.FirstEditionYear;
        //        title.Publisher = model.Publisher;

        //        string message;
        //        if (title.Publisher.PublisherId > 0)
        //        {
        //            if (title.save())
        //            {
        //                //after title is saved, the authors can be referenced
        //                foreach (Author a in model.Authors)
        //                    Title.addAuthor(a.Aid, title.TitleId);

        //                foreach (Topic t in model.Topics)
        //                    Title.addTopic(t.TopicId, title.TitleId);

        //                base.Message = new HttpMessage("Title has been added.", true);
        //                return RedirectToAction("Get", "Title", new { id = title.TitleId });
        //            }
        //            else
        //            {
        //                base.Message = new HttpMessage("Technical problems during save. Please try again.", false);
        //            }
        //        }
        //        else
        //        {
        //            base.Message = new HttpMessage("Publisher could not be added.", false);
        //        }
        //    }

        //    return View(model);
        //}
    }
}
